package com.example.phpclient.content;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.phpclient.DB.NotesDB;
import com.example.phpclient.R;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class Note extends AppCompatActivity implements View.OnClickListener {

    private Button textbtn,imgbtn,videobtn;
    private ListView lv;
    private Intent intent;//为了按钮点击事件的传值
    private Cursor cursor;

    private MyAdapter adapter;
    private NotesDB notesDB;
    private SQLiteDatabase dbReader;

//    private static final int WRITE_PERMISSION = 0*01;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_activity);
        initView();

    }
    //初始化操作
    public void initView(){
        lv =(ListView) findViewById(R.id.note_list);
        textbtn =(Button) findViewById(R.id.addtext);
        imgbtn =(Button) findViewById(R.id.addimg);
        videobtn =(Button) findViewById(R.id.addvideo);

        textbtn.setOnClickListener(this);
        imgbtn.setOnClickListener(this);
        videobtn.setOnClickListener(this);

        notesDB =new NotesDB(this);
        dbReader = notesDB.getWritableDatabase();

       lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               cursor.moveToPosition(i);
               Intent intent = new Intent(Note.this,SelectAct.class);
               intent.putExtra(NotesDB.ID,cursor.getInt(cursor.getColumnIndex(NotesDB.ID)));
               intent.putExtra(NotesDB.CONTENT,cursor.getString(cursor.getColumnIndex(NotesDB.CONTENT)));
               intent.putExtra(NotesDB.TIME,cursor.getString(cursor.getColumnIndex(NotesDB.TIME)));
               intent.putExtra(NotesDB.PATH,cursor.getString(cursor.getColumnIndex(NotesDB.PATH)));
               intent.putExtra(NotesDB.VIDEO,cursor.getString(cursor.getColumnIndex(NotesDB.VIDEO)));
                startActivity(intent);
           }
       });


    }
    //按钮的监听事件
    @Override
    public void onClick(View view) {
        intent = new Intent(this,AddContent.class);//实例化
        switch (view.getId()){
            //跳转

            case R.id.addtext:
                intent.putExtra("flag","1");
                startActivity(intent);
                break;

            case R.id.addimg:
                intent.putExtra("flag","2");
                startActivity(intent);
                break;

            case R.id.addvideo:
                intent.putExtra("flag","3");
                startActivity(intent);
                break;

        }


    }
    //从数据库里查询
    public void selectDB(){
        cursor =dbReader.query(NotesDB.TABLE_NAME,null,null,null,null,null,null);
        adapter = new MyAdapter(this,cursor);
        lv.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectDB();
    }

//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        if (requestCode == WRITE_PERMISSION){
//            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
//                Toast.makeText(this, "you must allow permission");
//                finish();
//            }
//        }
//    }
//    private void requestWritePermission(){
//        if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
//            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},WRITE_PERMISSION);
//        }


}
